package com.gxc.rapacity;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 拼接字符串使得字典顺序最小
 */
public class MinimumDictionaryOrder {

    public static class MyStringComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return (o1+o2).compareTo(o2+o1);
        }
    }

    public static String minimumDictionary(String[] arrays) {
        Arrays.sort(arrays, new MyStringComparator());

        StringBuffer buffer = new StringBuffer();
        for (String str : arrays) {
            buffer.append(str);
        }
        return buffer.toString();
    }

    public static void main(String[] args) {
        String[] str1 = {"jibw","ji","jp", "bw","ji","bw"};
        System.out.println(minimumDictionary(str1));

        String[] str2 = {"ba","b"};
        System.out.println(minimumDictionary(str2));
    }
}
